package com.leps.nq.op.controller;

import cn.hutool.core.bean.BeanUtil;
import com.alibaba.excel.EasyExcel;
import com.leps.nq.bu.service.IBuStationService;
import com.leps.nq.common.aspect.Log;
import com.leps.nq.common.enums.BusinessType;
import com.leps.nq.common.excel.ExcelPageReadListener2;
import com.leps.nq.common.util.FileUploadUtils;
import com.leps.nq.common.vo.PageVo;
import com.leps.nq.common.vo.R;
import com.leps.nq.common.vo.RBuilder;
import com.leps.nq.op.entity.OpWaterTest;
import com.leps.nq.op.service.IOpWaterTestService;
import com.leps.nq.op.vo.ExcelImportWaterTestVo;
import com.leps.nq.op.vo.WaterTestQueryModel;
import io.swagger.annotations.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 水样检测记录表 前端控制器
 * </p>
 *
 * @author hex
 * @since 2022-05-22
 */
@RestController
@RequestMapping("/op/op-water-test")
@Api(tags = "水样检测记录管理")
public class OpWaterTestController {
    @Autowired
    IOpWaterTestService waterTestService;
    @Autowired
    IBuStationService stationService;

    @GetMapping
    @ApiOperation("水样检测记录列表")
    public R<PageVo<OpWaterTest>> list(WaterTestQueryModel queryModel) {
        PageVo<OpWaterTest> pageVo = this.waterTestService.pageByQueryModel(queryModel);
        return RBuilder.ok(pageVo);
    }

    @GetMapping("/{id}")
    @ApiOperation("根据id查询水样检测记录")
    public R<OpWaterTest> getById(@PathVariable Long id) {
        return RBuilder.ok(this.waterTestService.getById(id));
    }

    @PostMapping
    @ApiOperation("新增水样检测记录")
    @Log(title = "水样检测记录", businessType = BusinessType.INSERT)
    public R<?> save(@RequestBody OpWaterTest waterTest) {
        return RBuilder.ofBoolean(this.waterTestService.save(waterTest));
    }

    @PutMapping
    @ApiOperation("修改水样检测记录")
    @Log(title = "水样检测记录", businessType = BusinessType.UPDATE)
    public R<?> update(@RequestBody OpWaterTest waterTest) {
        return RBuilder.ofBoolean(this.waterTestService.updateById(waterTest));
    }

    @DeleteMapping
    @ApiOperation("删除水样检测记录")
    @Log(title = "水样检测记录", businessType = BusinessType.DELETE)
    public R<?> remove(@RequestBody List<Long> ids) {
        boolean b = this.waterTestService.removeBatchByIds(ids);
        return RBuilder.ofBoolean(b);
    }

    @PostMapping("/import-excel")
    @ApiOperation(value = "批量导入excel水样检测记录")
    @Transactional(rollbackFor = Exception.class)
    @Log(title = "水样检测记录", businessType = BusinessType.IMPORT)
    public R importExcel(MultipartFile file) throws IOException {
        if (file == null || file.isEmpty()) throw new RuntimeException("请上传excel文件");
        // 存储excel文件
        FileUploadUtils.uploadUseOriginFileName(file);
        // 执行导入
        ExcelPageReadListener2<ExcelImportWaterTestVo> listener = new ExcelPageReadListener2<>(list -> {
            List<OpWaterTest> entities = new ArrayList<>();
            for (ExcelImportWaterTestVo vo : list) {
                OpWaterTest entity = BeanUtil.toBean(vo, OpWaterTest.class);
                entities.add(entity);
            }
            this.waterTestService.saveBatch(entities);
        });
        EasyExcel.read(file.getInputStream(), ExcelImportWaterTestVo.class, listener).sheet().doRead();
        return RBuilder.ok();
    }

}
